// pages/status/index.js
var app = getApp()
Page({
  data:{
      tabs: ["全部", "我的", "回复我", "我回应"],
      pageIndex:1,
      pageSize:10,
      activeIndex: 0,
      sliderOffset: 0,
      sliderLeft: 0,
      all_statuses:[],
      my_statuses:[],
      comment_statuses:[],
      my_comment_statuses:[]
  },
  onLoad:function(options){
    // this.loadStatuses(this, this.data.pageIndex, this.data.pageSize);
    this.getStatusByType(this.data.activeIndex, this, 1, this.data.pageSize);
    wx.stopPullDownRefresh();
  },
  tabClick: function (e) {
    var activeIndex = e.currentTarget.id;
        this.setData({
            sliderOffset: e.currentTarget.offsetLeft,
            activeIndex: activeIndex,
            pageIndex: 1,
            all_statuses:[],
            my_statuses:[],
            comment_statuses:[],
            my_comment_statuses:[]
        });
        this.getStatusByType(activeIndex, this, 1, this.data.pageSize);
    },
  onReady:function(){
    // 页面渲染完成
  },
  onShow:function(){
    // 页面显示
  },
  onHide:function(){
    // 页面隐藏
  },
  onUnload:function(){
    // 页面关闭
  },
  //下拉刷新
  onPullDownRefresh:function(){
    wx.showNavigationBarLoading();
    this.setData({
      pageIndex:1,
      all_statuses:[],
      my_statuses:[],
      comment_statuses:[],
      my_comment_statuses:[]
    });
    // this.getStatuses(this, this.data.pageIndex, this.data.pageSize)
    this.getStatusByType(this.data.activeIndex, this, this.data.pageIndex, this.data.pageSize);
  },
  //上拉加载更多
  onReachBottom:function(){
    this.setData({
      pageIndex:this.data.pageIndex + 1
    });
    // this.getAllStatuses(this, this.data.pageIndex, this.data.pageSize)
    this.getStatusByType(this.data.activeIndex, this, this.data.pageIndex, this.data.pageSize);
  },
  getStatusByType:function(activeIndex, that, pageIndex, pageSize) {
    switch (parseInt(activeIndex)) {
          case 0:
              this.getAllStatuses(that, pageIndex, pageSize);
              break;
          case 1:
              this.getMyStatuses(that, pageIndex, pageSize);
              break;
          case 2:
              this.getReplyMyStatuses(that, pageIndex, pageSize);
              break;
          case 3:
              this.getIReplyStatuses(that, pageIndex, pageSize);
              break;
      }
  },
  getAllStatuses: function(that, pageIndex, pageSize){
      //获取全部闪存列表
      wx.request({
          url: 'http://api.cnblogs.com/api/statuses/@all',
          data: {
              pageIndex:pageIndex,
              pageSize:pageSize,
              tag:'is'
          },
        header: {
            "Content-Type":"application/x-www-form-urlencoded",
            "Authorization":"Bearer " + app.accessToken
        },
        success: function(res) {
          var list = that.data.all_statuses;
          for (var i = 0; i < res.data.length; i++) {
              list.push(res.data[i]);
          }
          that.setData({
              all_statuses: list
          });
        },
        complete: function () {
            wx.hideNavigationBarLoading();
            wx.stopPullDownRefresh();
        }
      })
  },
  getMyStatuses: function(that, pageIndex, pageSize){
      //获取我的闪存列表
      wx.request({
          url: 'http://api.cnblogs.com/api/statuses/@my',
          data: {
              pageIndex:pageIndex,
              pageSize:pageSize,
              tag:'is'
          },
        header: {
            "Content-Type":"application/x-www-form-urlencoded",
            "Authorization":"Bearer " + app.accessToken
        },
        success: function(res) {
          var list = that.data.my_statuses;
          for (var i = 0; i < res.data.length; i++) {
              list.push(res.data[i]);
          }
          that.setData({
              my_statuses: list
          });
        },
        complete: function () {
            wx.hideNavigationBarLoading();
            wx.stopPullDownRefresh();
        }
      })
  },
  getReplyMyStatuses: function(that, pageIndex, pageSize){
      //获取回复我的闪存列表
      wx.request({
          url: 'http://api.cnblogs.com/api/statuses/@comment',
          data: {
              pageIndex:pageIndex,
              pageSize:pageSize,
              tag:'is'
          },
        header: {
            "Content-Type":"application/x-www-form-urlencoded",
            "Authorization":"Bearer " + app.accessToken
        },
        success: function(res) {
          var list = that.data.comment_statuses;
          for (var i = 0; i < res.data.length; i++) {
              list.push(res.data[i]);
          }
          that.setData({
              comment_statuses: list
          });
        },
        complete: function () {
            wx.hideNavigationBarLoading();
            wx.stopPullDownRefresh();
        }
      })
  },
  getMyReplyStatuses: function(that, pageIndex, pageSize){
      //获取我回复的闪存列表
      wx.request({
          url: 'http://api.cnblogs.com/api/statuses/@mycomment',
          data: {
              pageIndex:pageIndex,
              pageSize:pageSize,
              tag:'is'
          },
        header: {
            "Content-Type":"application/x-www-form-urlencoded",
            "Authorization":"Bearer " + app.accessToken
        },
        success: function(res) {
          var list = that.data.my_comment_statuses;
          for (var i = 0; i < res.data.length; i++) {
              list.push(res.data[i]);
          }
          that.setData({
              my_comment_statuses: list
          });
        },
        complete: function () {
            wx.hideNavigationBarLoading();
            wx.stopPullDownRefresh();
        }
      })
  },
})